package com.myslef.teaching_platfrom.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.myslef.teaching_platfrom.common.R;
import com.myslef.teaching_platfrom.domain.StudentCourse;
import com.myslef.teaching_platfrom.domain.course.Course;
import com.myslef.teaching_platfrom.domain.users.User;
import com.myslef.teaching_platfrom.service.CourseService;
import com.myslef.teaching_platfrom.service.StudentCourseService;
import com.myslef.teaching_platfrom.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/studentCourse")
@CrossOrigin(origins = "http://localhost:8080")
public class StudentCourseController {
    @Autowired
    StudentCourseService scService;

    @Autowired
    UserService userService;

    @Autowired
    CourseService courseService;

    @GetMapping("/{id}")
    public R getCourseById(@PathVariable Integer id){
        User u = userService.getById(id);
        if(u == null){
            return R.error("没有该用户");
        }
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper();
        List<Course> courseList = new ArrayList<>();
        switch (u.getLevel()){
            case 1:{
                LambdaQueryWrapper<StudentCourse> scQW = new LambdaQueryWrapper();
                scQW.eq(StudentCourse::getStudentId, id);
                List<StudentCourse> list = scService.list(scQW);
                List<Integer> ids = new ArrayList<>();
                list.forEach(item -> ids.add(item.getCourseId()));
                System.out.println("ids"+ids);
                if(ids.isEmpty())
                    return R.success(courseList);
                queryWrapper.in(Course::getId, ids);
                break;
            }
            case 2:{
                queryWrapper.eq(Course::getTeacherId, id);
                break;
            }
        }
//        System.out.println();
        courseList = courseService.list(queryWrapper);
        return R.success(courseList);
    }

    @PostMapping("/teacherInvite/{classId}")
    public R teacherInvite(@PathVariable Integer classId, @RequestBody User user){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUid, user.getUid()).eq(User::getUserName, user.getUserName());
        User one = userService.getOne(queryWrapper);
        if(one == null){
            return R.error("学号或姓名错误！");
        }
        LambdaQueryWrapper<StudentCourse> qw = new LambdaQueryWrapper<>();
        qw.eq(StudentCourse::getStudentId, one.getId()).eq(StudentCourse::getCourseId, classId);
        StudentCourse studentCourse = scService.getOne(qw);
        if(studentCourse != null){
            return R.error("该学生已加入课程！");
        }
        studentCourse = new StudentCourse();
        studentCourse.setCourseId(classId);
        studentCourse.setStudentId(one.getId());
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        studentCourse.setCreateTime(timestamp);
        boolean save = scService.save(studentCourse);
        if(save == true)
            return R.success("加入成功");
        else
            return R.error("添加失败");
    }

    @PostMapping("/invite")
    public R teacherInvite(@RequestBody Map map){
        String inviteId = map.get("inviteId").toString();
        Integer id = (Integer)map.get("id");
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId, id);
        User u = userService.getOne(queryWrapper);
        if(u == null){
            return R.error("查无此人");
        }

        LambdaQueryWrapper<Course> qwc = new LambdaQueryWrapper<>();
        qwc.eq(Course::getInviteId, inviteId);
        Course c = courseService.getOne(qwc);
        if(c == null){
            return R.error("邀请码错误");
        }

        LambdaQueryWrapper<StudentCourse> qw = new LambdaQueryWrapper<>();
        qw.eq(StudentCourse::getStudentId, u.getId()).eq(StudentCourse::getCourseId, c.getId());
        StudentCourse one = scService.getOne(qw);
        if(one != null){
            return R.error("该用户加入课程");
        }
        one = new StudentCourse();
        one.setCourseId(c.getId());
        one.setStudentId(u.getId());
        one.setCreateTime(new Timestamp(System.currentTimeMillis()));
        boolean save = scService.save(one);
        if(save == true)
            return R.success("加入成功");
        else
            return R.error("添加失败");
    }


    @GetMapping("/getStudentList/{id}")
    public R getStudentList(@PathVariable Integer id){
        List<User> studentList = scService.getStudentList(id);
        return R.success(studentList);
    }
    @DeleteMapping("/deleteSC")
    public R deleteSC(@RequestBody StudentCourse studentCourse){
        User user = userService.getById(studentCourse.getStudentId());
        if(user == null){
            return R.error("删除失败！");
        }
        LambdaUpdateWrapper<StudentCourse> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(StudentCourse::getStudentId, studentCourse.getStudentId())
                .eq(StudentCourse::getCourseId,studentCourse.getCourseId());
        boolean remove = scService.remove(updateWrapper);
        if(remove)
            return R.success("删除成功");
        else
            return R.error("删除失败");
    }

}
